package com.hyq.hm.hyperlandmark.tools;

import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;




/**
 * Created by 海米 on 2018/11/29.
 */

public class GLPoints {
    private FloatBuffer vertexBuffer;
    private int bufferLength = 106*2*4;
    private int programId = -1;
    private int aPositionHandle;

    private int[] vertexBuffers;

    /***
     * 使用OpenGLES 2.0画一个定义好的形状需要比较多的代码，因为你必须为图形渲染管线提供一大堆信息。特别的，你必须定义以下几个东西：
     * Vertex Shader - 用于渲染形状的顶点的OpenGLES 图形代码。
     * Fragment Shader - 用于渲染形状的外观（颜色或纹理）的OpenGLES 代码。
     * Program - 一个OpenGLES对象，包含了你想要用来绘制一个或多个形状的shader。
     */
    private String fragmentShader =
            "void main() {\n" +
            "    gl_FragColor = vec4(0.0,1.0,0.0,0.0);\n" +
            "}";
    private  String vertexShader = "attribute vec2 aPosition;\n" +
            "void main() {\n" +
            "    gl_Position = vec4(aPosition,0.0,0.0);\n" +
            "    gl_PointSize = 10.0;\n"+
            "}";
    public GLPoints(){
        vertexBuffer = ByteBuffer.allocateDirect(bufferLength)
                .order(ByteOrder.nativeOrder())
                .asFloatBuffer();
        vertexBuffer.position(0);
    }
    public void initPoints(){
        programId = ShaderUtils.createProgram(vertexShader, fragmentShader);
        aPositionHandle = GLES20.glGetAttribLocation(programId, "aPosition");

        vertexBuffers = new int[1];
        GLES20.glGenBuffers(1,vertexBuffers,0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffers[0]);
        GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, bufferLength, vertexBuffer,GLES20.GL_STATIC_DRAW);

        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
    }
    public void setPoints(float[] points){
        vertexBuffer.rewind();//Buffer.rewind()将position设回0
        vertexBuffer.put(points);
        vertexBuffer.position(0);
    }


    public void drawPoints(){
        GLES20.glUseProgram(programId);//使用着色器程序绘制图形
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffers[0]);
        GLES20.glBufferSubData(GLES20.GL_ARRAY_BUFFER,0,bufferLength,vertexBuffer);
        GLES20.glEnableVertexAttribArray(aPositionHandle);
        GLES20.glVertexAttribPointer(aPositionHandle, 2, GLES20.GL_FLOAT, false,
                0, 0);
        GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
        GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, 10);

    }

    public void release(){
        GLES20.glDeleteProgram(programId);
        GLES20.glDeleteBuffers(1,vertexBuffers,0);
    }
}
